package com.uit.se6.mydiary.util;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ImageSpan;

import com.uit.se6.mydiary.R;


public class EmotionText {

	private HashMap<String, Integer> emoticons = new HashMap<String, Integer>();
	private static EmotionText instance;

	private EmotionText() {
		emoticons.put("*X-|*", R.drawable.e_angry);//6
		emoticons.put("*=.=*", R.drawable.e_annoyed);//7
		emoticons.put("*XD*", R.drawable.e_biggrin);//8
		emoticons.put("*:D*", R.drawable.e_biggrin_eye);//9
		emoticons.put("*:'>*", R.drawable.e_blushing);//10
		emoticons.put("*:-O*", R.drawable.e_confused);//11
		emoticons.put("*:-PP*", R.drawable.e_crazy);//12
		emoticons.put("*:((*", R.drawable.e_cry);//13
		emoticons.put("*:((o*", R.drawable.e_cry_scream);//14
		emoticons.put("*:DD*", R.drawable.e_excited);//15
		emoticons.put("*X-((*", R.drawable.e_fierce);//16
		emoticons.put("*XP*", R.drawable.e_hungry);//17
		emoticons.put("*:-((*", R.drawable.e_hurt);//18
		emoticons.put("*:X*", R.drawable.e_kiss);//19
		emoticons.put("*:))*", R.drawable.e_laugh);//20
		emoticons.put("*::X*", R.drawable.e_lovestruck);//21
		emoticons.put("*=-*", R.drawable.e_raisedeyebrow);//22.
		emoticons.put("*:(*", R.drawable.e_sad);//23
		emoticons.put("*:)*", R.drawable.e1);//1
		emoticons.put("*:|*", R.drawable.e2);//2
		emoticons.put("*oo*", R.drawable.e3);//3
		emoticons.put("*ooo*", R.drawable.e4);//4
		emoticons.put("*X-P*", R.drawable.e5);//5
//		emoticons.put("*:o?*", R.drawable.e6);
//		emoticons.put("*:<(*", R.drawable.e_tired);
//		emoticons.put("*:-P*", R.drawable.e_tongue);
//		emoticons.put("*:oo*", R.drawable.e_unbelievable);
//		emoticons.put("*:<*", R.drawable.e_uncomfortable);
	}
	public static EmotionText getIns() {
		if (instance == null) {
			instance = new EmotionText();
		}
		return instance;
	}
	public HashMap<String, Integer> getEmotionsTextHashMap() {
		return emoticons;
	}
	@SuppressLint("DefaultLocale")
	public static Spannable getSmiledText(Activity context, String text,
			int imgSize) {
		SpannableStringBuilder builder = new SpannableStringBuilder(text);
		if ((EmotionText.getIns().getEmotionsTextHashMap()).size() > 0) {
			for (int index = 0; index < builder.length(); index++) {
				if (Character.toString(builder.charAt(index)).equals("*")) {
					for (Map.Entry<String, Integer> entry : EmotionText
							.getIns().getEmotionsTextHashMap().entrySet()) {
						int length = entry.getKey().length();
						if (index + length > builder.length())
							continue;
						String sTemp = builder.subSequence(index,
								index + length).toString();
						if (sTemp.toUpperCase(Locale.US).equals(
								entry.getKey().toUpperCase(Locale.US))) {
							Bitmap origin = BitmapFactory.decodeResource(
									context.getResources(), entry.getValue());
							Bitmap resizedBitmap = UtilBitmap.getResizedBitmap(
									origin, imgSize, imgSize);

							ImageSpan imgSpan = new ImageSpan(context,
									resizedBitmap);
							builder.setSpan(imgSpan, index, index + length,
									Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

							index += length - 1;
							break;
						}
					}
				}
			}
		}
		return builder;
	}

}
